[% setvar title Index Attribute %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Index Attribute</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: David Nicol &lt;<a href='mailto:perl6rfc@davidnicol.com'>perl6rfc@davidnicol.com</a>&gt;
  Date: 19 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 262
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>An attribute <code>:n</code> is defined which means the index or key of a
contained item.  This attribute is useful when an index number is needed
within a <code>foreach</code> statement, it also may be used inside container-like
abstractions
which dynamicly generate their contents.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<pre>
	foreach $item (@items){
		#print &quot;$item was at location &quot;,$item:n,&quot;\n&quot;;
		print &quot;$item was at location ${item:n}\n&quot;;
	};</pre>
<p>Given acceptance of several other iffy proposals,</p>
<pre>	@Nplus1 = lazy sub { return $_:1 + 1 }; # or something like this</pre>
<a name='DISCUSSION'></a><h1>DISCUSSION</h1>
<p>When this was suggested on the language-objects list,
there was discussion of the name of the attribute, with <code> :i </code> being
suggested as well as <code> :index </code></p>
<p>We also need access to attributes of variables as a standard language
feature, that is implicit here.</p>
<p>Another possibility would be to use <code> $[ </code> for the attribute, which
makes sense if we allow <code> $[ </code> to be set individually per container.</p>
<p>Use of brackets in variable names is a Big Hassle for parsing:</p>
<pre>		print &quot;$item was at location ${item:[}\n&quot;;</pre>
<p>is parseable if colon is elevated to the same level of immediacy,
in terms of eating subsequent tokens, as dollar-sign now has.</p>
<p>Use of left-bracket as the index attribute matches very well with
the situation of pulling sequential items out of a function pretending
to be a list, though, as internal to the code
we can refer to $[, &quot;The index of the first item&quot;, to get the index
of the current item after several items have been shifted off (and
the specific $[ attribute incremented each time.)</p>
<pre>	@Nplus1 = lazy sub { return $[ + 1 }; # or something like this</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Within looping constructs, we only need to track this if it is used.
We may be using an implied c-like <code>for</code> when we do a looping construct
anyway, in which case we already have the information, and just need some
Perl syntax for sharing it with user code.  In linked-list kinds of
situations, we will need to identify the need to track this info and
then track it for the user.</p>
<p>In terms of $[ we would make the familiar and vaguely deprecable $[ be
the global to which the one specific to an array instance defaults to
or starts at.  (which may deserve it's own RFC it is true.)</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>John McNamara left this radical suggestion out of RFC 120.</p>
</div>
